package edu.fjut.se1603.yuan.dao.impl;

import edu.fjut.se1603.yuan.dao.BaseDao;
import edu.fjut.se1603.yuan.dao.PetDao;
import edu.fjut.se1603.yuan.entity.Pet;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class PetDaoImpl extends BaseDao implements PetDao {

    @Override
    public List<Pet> getAllPet() {
        List<Pet> pets = null;
        try {
            Statement statement = conn.createStatement();
            ResultSet set = statement.executeQuery("select * from pet");

            pets = new ArrayList<>();
            while (set.next()) {
                int id = set.getInt("ID");
                String name = set.getString("NAME");
                double price = set.getDouble("PRICE");
                int ownerId = set.getInt("OWNER_ID");
                int storeId = set.getInt("STORE_ID");
                pets.add(new Pet(id, ownerId, storeId, name, price));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return pets;
    }

    @Override
    public List<Pet> getAllFreePet() {
        List<Pet> pets = null;
        try {
            Statement statement = conn.createStatement();
            ResultSet set = statement.executeQuery("select ID,NAME,STORE_ID,PRICE from pet where OWNER_ID is null ");
            pets = new ArrayList<>();
            while (set.next()) {
                Pet pet = new Pet();
                pet.setId(set.getInt("ID"));
                pet.setName(set.getString("NAME"));
                pet.setPrice(set.getDouble("PRICE"));
                pet.setStoreId(set.getInt("STORE_ID"));
                pets.add(pet);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return pets;
    }

    @Override
    public List<Pet> getOwnerPet(int petOwnerId) {
        List<Pet> pets = null;
        try {
            PreparedStatement preparedStatement = conn.prepareStatement("select ID,NAME,STORE_ID,PRICE  from pet where OWNER_ID = ? ");
            preparedStatement.setInt(1, petOwnerId);
            ResultSet set = preparedStatement.executeQuery();
            pets = new ArrayList<>();
            while (set.next()) {
                Pet pet = new Pet();
                pet.setId(set.getInt("ID"));
                pet.setName(set.getString("NAME"));
                pet.setPrice(set.getDouble("PRICE"));
                pet.setStoreId(set.getInt("STORE_ID"));
                pets.add(pet);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return pets;
    }

    @Override
    public double getPetPrice(int petId) {
        double price = 0;
        try {
            PreparedStatement preparedStatement = conn.prepareStatement("select PRICE from pet where ID=?");
            preparedStatement.setInt(1, petId);
            ResultSet set = preparedStatement.executeQuery();
            if (set.next()) {
                price = set.getDouble("PRICE");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return price;
    }

    @Override
    public int updateOwner(int petId, Integer petOwnerId) {
        int flag = 0;
        try {
            PreparedStatement preparedStatement = conn.prepareStatement("update  pet set OWNER_ID= ? where ID=?");
            preparedStatement.setInt(1, petOwnerId);
            preparedStatement.setInt(2, petId);
            flag = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return flag;
    }

    @Override
    public int updateStore(int petId, Integer petStoreId) {
        int flag = 0;
        try {
            PreparedStatement preparedStatement = conn.prepareStatement("update  pet set OWNER_ID=null,STORE_ID=? where ID=?");
            preparedStatement.setInt(1, petStoreId);
            preparedStatement.setInt(2, petId);
            flag = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return flag;
    }

    @Override
    public int addPet(String name, double price, int petStoreId) {
        int flag = 0;
        try {
            PreparedStatement preparedStatement = conn.prepareStatement("insert into pet (name, price, store_id) values (?,?,?);");
            preparedStatement.setString(1, name);
            preparedStatement.setDouble(2, price);
            preparedStatement.setInt(3, petStoreId);
            flag = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return flag;
    }

    @Override
    public int getStorePet(int storeId) {
        try {
            PreparedStatement preparedStatement = conn.prepareStatement("select ID,NAME,PRICE from pet where OWNER_ID is null and STORE_ID=?");
            preparedStatement.setInt(1, storeId);
            ResultSet set = preparedStatement.executeQuery();
            while (set.next()) {
                System.out.println(set.getInt("ID")+" "+set.getString("NAME")+" "+set.getDouble("PRICE"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}

